package io.dy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.dy.mapper.AccountMapper;
import io.dy.model.AccountModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author dmj
 * @date 2023年10月12日 下午9:46
 */
@Service
@RequiredArgsConstructor
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountModel> implements AccountService {

    private final AccountMapper accountMapper;


    @Override
    @Transactional
    public Boolean deductionBalance(Long accountId, BigDecimal balance) {
        AccountModel accountModel = accountMapper.selectById(accountId);
        if (accountModel.getAccountLeft().subtract(balance).compareTo(BigDecimal.ZERO) < 0) {
            throw new RuntimeException("账户金额不能小于0");
        }
        accountModel.setAccountLeft(accountModel.getAccountLeft().subtract(balance));
        accountMapper.updateById(accountModel);
        return true;
    }

    @Override
    public Boolean selectLeftAmtById(Long id) {
        return null;
    }
}
